<?php
namespace app\admin\controller;

use app\admin\model\Member as MemberModel;
use app\common\controller\AdminBaseController;
use think\Config;
use think\Db;

/**
 * 用户管理
 * Class Member
 * @package app\admin\controller
 */
class MemberController extends AdminBaseController {
    protected $member_model;

    protected function _initialize() {
        parent::_initialize();
        $this->member_model = new MemberModel();
    }

    /**
     * 用户管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($page = 1) {
    	$map = [];
		if(request()->isPost()){
		 	$param = input('post.');
			//用户名称
			if($param['name']){
				$map['nick_name'] = ['like', '%'.$param['name'].'%'];
			}
			//手机号码
			if($param['mobile']){
				$map['mobile'] = ['like', '%'.$param['mobile'].'%'];
			}
			//用户状态查询
			if($param['status'] != 2){
				$map['status'] = $param['status'];
			}
			//创建时间
			if($param['add_time']){
				$map['add_time'] = $param['add_time'];
			}
			//结束时间
			if($param['end_time']){
				$map['end_time'] = $param['end_time'];
			}
	 	}

		$member_list = Db::table('long_member')->where($map)->order('id DESC')->paginate(10);
        return $this->fetch('index', ['list' => $member_list]);
    }

    /**
     * 添加用户
     * @return mixed
     */
    public function add() {
        return $this->fetch();
    }
	
	/**
     * [export导出]
     * @author [longx] 
     */
    public function export(){
    	$map = [];
		if(request()->isPost()){
		 	$param = input('post.');
			//用户名称
			if($param['name']){
				$map['nick_name'] = ['like', '%'.$param['name'].'%'];
			}
			//手机号码
			if($param['mobile']){
				$map['mobile'] = ['like', '%'.$param['mobile'].'%'];
			}
			//用户状态查询
			if($param['status'] != 2){
				$map['status'] = $param['status'];
			}
			//创建时间
			if($param['add_time']){
				$map['add_time'] = $param['add_time'];
			}
			//结束时间
			if($param['end_time']){
				$map['end_time'] = $param['end_time'];
			}
	 	}

		$member_list = Db::table('long_member')->where($map)->order('id DESC')->paginate(100);
		
        $str = "用户编号,姓名,手机号码,余额,果币,状态,创建时间,\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($member_list as $k=>$row){
            $id = iconv('utf-8','gbk',$row['id']); //中文转码 
            $nick_name = iconv('utf-8','gbk',$row['nick_name']); 			
			$mobile = iconv('utf-8','gbk',$row['mobile']);
			$credit2 = iconv('utf-8','gbk',$row['credit2']==0?0:$row['credit2']);
			$credit1 = iconv('utf-8','gbk',$row['credit1']==0?0:$row['credit1']);			
			if($row['status'] == 1){
				$statusname = '激活';
			}else{
				$statusname = '未激活';
			}
			$status = iconv('utf-8','gbk',$statusname);
			$add_time = iconv('utf-8','gbk',$row['add_time']);
						                           
            $str .= $id.",".$nick_name.",".$mobile.",".$credit2.",".$credit1.",".$status.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='会员列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
     }

    /**
     * 保存用户
     */
    public function save() {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'Member');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['password'] = md5_salt($data['password']);
                if ($this->member_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     *更改状态
     * @param $id
     * @return mixed
     */
    public function edit($id,$status,$page = 1) {
    	if($status == '1'){
       		$data['status']= '0';
       	}
       if($status == '0'){
       		$data['status']= '1';
       	}
		if ($this->member_model->allowField(true)->save($data,['id'=>$id]) !== false) {
        	$this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
	}


    /**
     * 更新用户
     * @param $id
     */
    public function update($id) {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'Member');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if (!empty($data['password'])) {
                    $data['password'] = md5_salt($data['password']);
                }
                if ($this->member_model->allowField(true)->save($data,['id'=>$id]) !== false) {
                    $this->success('更新成功', url('member/index'));
                } else {
                    $this->error('更新失败');
                }
            }
        }
		return $this->fetch('update', ['model' => $this->member_model->find($id)]);
    }
	
    /**
     * 删除用户
     * @param $id
     */
    public function delete($id) {
        if ($this->member_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
     public function setup($id,$pid) {
     	$member=$this->member_model->find($id);
     	$me=$member['pid'];
         var_dump($pid);
                if ($this->member_model->find($pid) !== false) {
                	
                    $this->member_model->save(['pid' => '78'],['id'=>$id]);
                                        var_dump($member);die;
                    $this->success('成功');

                } else {
                    $this->error('更新失败');
                }
            
		return $this->fetch('set', ['list' => $this->member_model->paginate(10)]);
       
    }
    /**
     *更改状态
     * @param $id
     * @return mixed
     */
    public function edit2($id,$agent_type,$page = 1) {
    	if($agent_type == '1'){
       		$data['agent_type']= '2';
       	}
      
		if ($this->member_model->allowField(true)->save($data,['id'=>$id]) !== false) {
        	$this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
	}
	/**
     * 充值余额
     * @param int    $page
     * @return mixed
     */
    public function credit($id,$integral) {
    	    $a='';
       		$member=$this->member_model->find($id);
       		$a=$member['credit2']+$integral;
		if ($this->member_model->allowField(true)->save(['credit2'=>$a],['id'=>$id]) !== false) {
        	$this->success("操作成功");
        } else {
            $this->error('操作失败');
        }
    }
    /**
     * 充值积分
     * @param int    $page
     * @return mixed
     */
    public function integral($id,$integral) {
    	    $a='';
       		$member=$this->member_model->find($id);
       		$a=$member['credit1']+$integral;
		if ($this->member_model->allowField(true)->save(['credit1'=>$a],['id'=>$id]) !== false) {
        	$this->success("操作成功");
        } else {
            $this->error('操作失败');
        }
    }
   
}